local Utils = {};
Utils.HttpUtils = function()
	local http = require("socket.http");
	local ltn12 = require("ltn12");
	local self = {};
	self.HttpRequest = function(url,header,postData)
		local responseText = {};
		local res,code,responseHeader;

		if(postData == nil) then
			res,code,responseHeader = http.request{
				url = url,
				method = "GET",
				headers = header,
				sink = ltn12.sink.table(responseText)
			};
		else
			res,code,responseHeader = http.request{
				url = url,
				method = "POST",
				headers = header,
				source = ltn12.source.string(postData),
				sink = ltn12.sink.table(responseText)
			};
		end

		return code,table.concat(responseText),responseHeader;
	end
	
	return self;
end

Utils.HttpUtils2 = function()
	local http = require("resty.http");
	local httpc = http:new();
	local self = {};
	local res,err;
	httpc:set_timeout(3000);
	self.HttpRequest = function(url,header,postData)
		if(postData == nil) then
			res,err = httpc:request_uri(url,{
				method = "GET",
				headers = header
			});
		else
			res,err = httpc:request(url,{
				method = "POST",
				body = postData,
				headers = header
			});
		end
		return res.status,res.body;
	end

	return self;
end
-- local header = {};
-- header["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.64";
-- local httputil = HttpUtils();
-- local code,resp = httputil.HttpRequest("http://www.cditv.cn/api.php?op=live&type=live&catid=192&id=1",header,nil);
-- print(code,resp)

Utils.HttpsUtils = function()
	local self = {};
	--使用curl进行https请求
	self.HttpsRequest = function(url,headers,postData)
		local cmd = "curl -i ";
		if(headers ~= nil) then
			for key,value in pairs(headers) do
				cmd = cmd .."-H \""..key..":"..value.."\" ";
			end
		end

		if(postData ~= nil) then
			cmd = cmd.."-X POST --data \""..postData.."\" ";
		end

		cmd = cmd.."\""..url.."\"";

		local f = io.popen(cmd,"r");
		local content = f:read("*a");
		print(content);
		local contentArr = Utils.StringUtils().split(content,"\n");
		local respHeader = {};
		local resultLen = 0;
		for i=2,#contentArr-2,1 do
			if(contentArr[i] == "") then
				resultLen = #contentArr - i;
				break;
			end
			local h = Utils.StringUtils().split(contentArr[i],":");
			respHeader[h[1]] = h[2];
		end
		local result = "";
		for i = resultLen,0,-1 do
			result = result..contentArr[#contentArr-i];
		end
		local code = contentArr[1];
		return result,respHeader,code;
	end

	self.OPTIONSRequest = function(url,headers)
		local cmd = "curl -i -X OPTIONS ";
		if(headers ~= nil) then
			for key,value in pairs(headers) do
				cmd = cmd .."-H \""..key..":"..value.."\" ";
			end
		end
		cmd = cmd.."\""..url.."\"";

		local f = io.popen(cmd,"r");
		local content = f:read("*a");
		print(content);
		local contentArr = Utils.StringUtils().split(content,"\n");
		local respHeader = {};
		local resultLen = 0;
		for i=2,#contentArr-2,1 do
			if(contentArr[i] == "") then
				resultLen = #contentArr - i;
				break;
			end
			local h = Utils.StringUtils().split(contentArr[i],":");
			respHeader[h[1]] = h[2];
		end
		local result = "";
		for i = resultLen,0,-1 do
			result = result..contentArr[#contentArr-i];
		end
		local code = contentArr[1];
		return result,respHeader,code;
	end

	return self;
end

Utils.RedisUtils = function()
	local redis = require("resty.redis");
	local self = {};
	local client = redis.connect("127.0.0.1","6379");

	self.set = function(key,value)
		return client:set(key,value);
	end

	self.pexpire = function(key,times)
		return client:pexpire(key,times);
	end

	self.get = function(key)
		return client:get(key);
	end

	self.del = function(key)
		return client:del(key);
	end

	return self;

end


Utils.StringUtils = function()
	local self = {};
	local b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

	self.split = function(str,sp)
		local result = {};
		local len = string.len(str);
		local i = 1;

		if(sp == nil or sp == "") then
			while(i<=len) do
				result[i] = string.sub(str,i,i);
				print(string.byte(result[i]));
				i = i+1;
			end
			return result;
		end

		local idx;
		local j = 1;
		while(i<=len) do
			idx = string.find(str,sp,i);
			if(idx == nil) then
				idx = len + 1;
			end
			result[j] = string.sub(str,i,idx-1);
			i = idx + 1;
			j = j + 1 ;
		end

		return result;
	end

	self.encodeURI = function(s)
    	s = string.gsub(s, "([%p])", function(c) return string.format("%%%02X", string.byte(c)) end)
   		return string.gsub(s, " ", "+")
	end

	self.base64Decode = function(data)
		data = string.gsub(data, '[^'..b64chars..'=]', '')
		return (data:gsub('.', function(x)
			if (x == '=') then return '' end
			local r,f='',(b64chars:find(x)-1)
			for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
			return r;
		end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
			if (#x ~= 8) then return '' end
			local c=0
			for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
			    return string.char(c)
		end))
	end

	self.base64Encode = function(data)
		return ((data:gsub('.', function(x) 
			local r,b='',x:byte()
			for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
			return r;
		end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
			if (#x < 6) then return '' end
			local c=0
			for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
			return b64chars:sub(c+1,c+1)
		end)..({ '', '==', '=' })[#data%3+1])
	end

	return self;
end

--local redisClient = RedisUtils();
--redisClient.set("test","test112");
--print(redisClient.get("test"));
--redisClient.del("test");
--print(redisClient.get("test"));

return Utils;